
package com.bear.cmcc.NetTestHTTP.service;

import com.bear.cmcc.NetTestHTTP.component.PowerLock;
import com.bear.cmcc.NetTestHTTP.component.json.request.SubmitTestinfoRequest;
import com.bear.cmcc.NetTestHTTP.component.json.respond.SubmitTestinfoRespond;
import com.bear.cmcc.NetTestHTTP.constant.FtpConstant;
import com.bear.cmcc.NetTestHTTP.constant.HttpConstant;
import com.bear.cmcc.NetTestHTTP.constant.MyCommonConstant;
import com.bear.cmcc.NetTestHTTP.httpUtil.HttpUtil;
import com.bear.cmcc.NetTestHTTP.util.MyCommonUtil;
import com.bear.cmcc.NetTestHTTP.util.NetTestUtil;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import java.io.File;
import java.util.ArrayList;

/**
 * 把测试日志上传到全国和福建的FTP服务器。
 * 
 * @author suchangjun
 */
public class NetSpeedLogUpLoadService extends Service {

    private PowerLock powerLock;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        try {
            powerLock = new PowerLock(this, "NetSpeedLogUpLoadService");
            Log.d("NetSpeedLogUpLoadService", "NetSpeedLogUpLoadService startUploadLog");
            startUpLoadFile(this);
        } catch (Exception e) {
            if (e != null) {
                Log.d("NetSpeedLogUpLoadService",
                        "NetSpeedLogUpLoadService startException" + e.getMessage());
            }
        }
    }

    /**
     * 启动一个上传文件进程。
     * 
     * @param context
     */
    private void startUpLoadFile(final Context context) {

        if (powerLock != null) {
            powerLock.acquireWakeLock();
        }
        Thread thread = new Thread() {

            @Override
            public void run() {
                boolean success = false;
                boolean successFj = false;
                try {
                    // success =
                    // FtpUtil.upLoadAllFileToComFTP(NetSpeedLogUpLoadService.this);
                    // successFj =
                    // FtpUtil.upLoadAllFileToFjFTP(NetSpeedLogUpLoadService.this);
                    if (MyCommonUtil.isNetworkAviable(context)) {
                        File file = new File(context.getFilesDir() + "/"
                                + FtpConstant.FtpFileNames.logsaveaddressfj);
                        if (file.exists()) {
                            ArrayList<SubmitTestinfoRequest> result = null;
                            SubmitTestinfoRespond respond = null;
                            // 一个个文件上传
                            ArrayList<File> wantUpFile = new ArrayList<File>();
                            if (file.isDirectory()) {
                                for (File element : file.listFiles()) {
                                    result = NetTestUtil.changeFileToSubmitObject(element,context);
                                    if (result != null && result.size() > 0) {
                                        respond = HttpUtil.getSubmitTestinfo(context, result);
                                        if (respond != null
                                                && respond.getErrorCode() == HttpConstant.CODE_SUCCESS) {
                                            wantUpFile.add(element);
                                        }
                                    }
                                }

                                if (wantUpFile.size() == file.listFiles().length) {
                                    successFj = true;
                                }

                                for (File successFile : wantUpFile) {
                                    successFile.delete();
                                }

                            } else {
                                result = NetTestUtil.changeFileToSubmitObject(file,context);
                                if (result != null && result.size() > 0) {
                                    respond = HttpUtil.getSubmitTestinfo(context, result);
                                    if (respond != null
                                            && respond.getErrorCode() == HttpConstant.CODE_SUCCESS) {
                                        successFj = true;
                                        file.delete();
                                    }
                                }
                            }

                        }
                    }
                } catch (Exception e) {
                    if (e != null) {
                        e.printStackTrace();
                    }
                } finally {
                    Log.d("NetSpeedLogUpLoadService", "上传文件成功与否 "+successFj);
                    sendFileDownBroad(success, successFj);
                    if (powerLock != null) {
                        powerLock.releaseWakeLock();
                    }
                    stopSelf();// 数据上传完成，把自己这个服务停止。
                }

            }
        };
        thread.start();

    }

    /**
     * 文件上传完成发送一个广播通知activity.
     * 
     * @param AllFileSuccess 全国排名数据上传成功与否。
     * @param fjFileSuccess 福建小区排名上传成功与否。
     */
    private void sendFileDownBroad(boolean AllFileSuccess, boolean fjFileSuccess) {
        try {
            Intent intent = new Intent(
                    MyCommonConstant.PrivateActionName.NET_WORK_TEST_DATA_UP_LOAD);
            intent.putExtra(MyCommonConstant.PrivateActionName.ALL_DATA_SUCCESS, AllFileSuccess);
            intent.putExtra(MyCommonConstant.PrivateActionName.FJ_DATA_SUCCESS, fjFileSuccess);

            sendBroadcast(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void onDestroy() {
        try {
            if (powerLock != null) {
                powerLock.releaseWakeLock();
            }
            powerLock = null;
        } catch (Exception e) {
            if (e != null) {
                e.printStackTrace();
            }
        }
        super.onDestroy();
    }

}
